﻿using UnityEngine;
using UnityEngine.UI;

public class BubbleShadow: MonoBehaviour
{
    private Vector2 fireVector;
    public int startNumber;
    public int number;
    public long id;

    private SpriteRenderer _renderer;

    public Text text;

    private float bubbleSize = 1;

    private void Awake()
    {
        this._renderer = this.GetComponent<SpriteRenderer>();
        number = NumberManager.Instance.GetANewNumber();
        text = this.gameObject.transform.Find("Text").GetComponent<Text>();
    }

    private void Update()
    {
        SetNumber();
        ResetScale();
    }

    public int SetNumber()
    {
        this._renderer.sprite = BubbleSpawner.Instance.GetSpriteByNumber(number);
        this.text.text = this.GetValue(this.number).ToString();
        if (number < 7)
        {
            this.text.fontSize = 65;
        }
        else if (number >= 7 && number < 10) 
        {
            this.text.fontSize = 55;
        }
        else
        {
            this.text.fontSize = 45;
        }

        return GetValue(number);
    }

    public void ResetScale()
    {
        this.transform.localScale = Vector3.one * this.GetSize(this.number);
    }

    private int GetValue(int _number)
    {
        return (int) Mathf.Pow(2f, _number);
    }

    private float GetSize(int _number)
    {
        float size = 0.55f;
        if (_number == 1)
        {
            return size;
        }

        for (int i = 1; i < _number; ++i)
        {
            if (i < 4)
                size += 0.05f;
            else
                size += 0.04f;
        }

        bubbleSize = size;
        return size;
    }

    private void OnTriggerStay2D(Collider2D coll)
    {
        if (coll.gameObject.tag.Equals("bubble")) 
        {
            //if (this.GetComponent<SpriteRenderer>().color.a == 1)
            {
                Destroy(this.gameObject);
                //BornShadow.instance.SpawnShadowBubble();
            }
        }
    }
    
}